package util;

/**
 * Created by Tecnocom on 1/19/2015.
 */
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.gonfre.quientelo.MyActivity;
import com.gonfre.quientelo.R;
import com.gonfre.quientelo.RegisterSellerActivity;
import com.gonfre.quientelo.RequestPriceActivity;

public class AutocompleteCustomArrayAdapter extends ArrayAdapter<MyDBObject> {

    Context mContext;
    int layoutResourceId;
    MyDBObject data[] = null;
    String writtenText;

    public AutocompleteCustomArrayAdapter(Context mContext, int layoutResourceId, MyDBObject[] data, String writtenText) {

        super(mContext, layoutResourceId, data);

        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
        this.writtenText = writtenText;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try{

            /*
             * The convertView argument is essentially a "ScrapView" as described is Lucas post
             * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
             * It will have a non-null value when ListView is asking you recycle the row layout.
             * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
             */
            if(convertView==null){
                // inflate the layout
                LayoutInflater inflater = ((MyActivity) mContext).getLayoutInflater();
                convertView = inflater.inflate(layoutResourceId, parent, false);
                Log.i("Trace AutocompleteCustomArrayAdapter 'getView'", "convertView IS NULL");
                // object item based on the position
                MyDBObject objectItem = data[position];

                //Format text
                SpannableString text = new SpannableString(objectItem.objectName);

                //Get Position of written text
                if (this.writtenText != null) {
                    int pos = objectItem.objectName.toUpperCase().indexOf(this.writtenText.toUpperCase());
                    int pos_e = pos + this.writtenText.length();
                    text.setSpan(new StyleSpan(Typeface.BOLD), pos, pos_e, 0);
                    text.setSpan(new RelativeSizeSpan(1.1f), pos, pos_e, 0);
                }

                // get the TextView and then set the text (item name) and tag (item ID) values
                TextView textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
                //textViewItem.setText(objectItem.objectName);
                textViewItem.setText(text, TextView.BufferType.SPANNABLE);
                textViewItem.setId(Integer.parseInt(objectItem.objectId));

                // in case you want to add some style, you can do something like:
                //textViewItem.setBackgroundColor(Color.CYAN);
            }else{
                Log.i("Trace AutocompleteCustomArrayAdapter 'getView'", "convertView IS NOT NULL");
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;

    }
}
